﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace MyPhotoSample
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            menuView.DropDown = ctxMenuPhoto;
            SetStatusLabel(null);
        }

        private void loadMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Title = "Choose Pictures";
            dlg.Filter = "jpg files (*.jpg)|*.jpg" + "|All files (*.*)|*.*";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    pbxPhoto.Image = new Bitmap(dlg.OpenFile());
                    SetStatusLabel(dlg.FileName);
                }
                catch (ArgumentException ex)
                {
                    MessageBox.Show("Unable to load file: {0}" + ex.Message);
                    pbxPhoto.Image = null;
                }

            }
            dlg.Dispose();
        }

        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void menuImag_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            ToolStripItem item = e.ClickedItem;
            string enumVal = item.Tag as string;
            if (enumVal != null)
            {
                pbxPhoto.SizeMode = (PictureBoxSizeMode)Enum.Parse(typeof(PictureBoxSizeMode),enumVal);
            }
        }

        private void menuImag_DropDownOpening(object sender, EventArgs e)
        {
            ToolStripDropDownItem parent = sender as ToolStripDropDownItem;
            if (parent != null)
            {
                string enumVal = pbxPhoto.SizeMode.ToString();
                foreach (ToolStripMenuItem item
                             in parent.DropDownItems)
                {
                    item.Enabled = (pbxPhoto.Image != null);
                    item.Checked = item.Tag.Equals(enumVal);
                }
            }
        }

        private void SetStatusLabel(string filePath)
        {
            if (pbxPhoto.Image != null)
            {
                statusInfo.Text = filePath;
                statusImageSize.Text = string.Format("{0:#} x {1:#}",pbxPhoto.Image.Width,pbxPhoto.Image.Height);
            }
            else
            {
                statusInfo.Text = null;
                statusImageSize.Text = null;
                statusAlbumPos.Text = null;
            }
        }

        private void testToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (TestDialog dlg = new TestDialog())
            {
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    statusInfo.Text = "OK Pressed";
                }
                else
                {
                    statusInfo.Text = "Cancel Pressed";
                }
            }
        }
    }
}
